<script>
import Vue from "vue";
import websocket from "@i5920/uni-socket";
import updater from "@/utils/uni-updater";
import config from "@/config";
import store from "@/store";
export default {
  onLaunch: function() {
    this.initPush();
    this.initBar();
    // 初始化socket配置
    this.initWebSocket();
    // #ifdef APP-PLUS
    updater
      .checkUpdate()
      .then(result => {
        if (!result) return;
        if (result.force) {
          // 强制更新
          uni.navigateTo({
            url: "/pages/common/appUpdate"
          });
          return;
        }
        uni.showModal({
          title: "更新提示",
          content: `新版本v${result.version}已经准备好了,是否立即更新?`,
          confirmText: "更新",
          cancelText: "下次吧",
          success: res => {
            if (res.confirm) {
              uni.navigateTo({
                url: "/pages/common/appUpdate"
              });
            }
          }
        });
      })
      .catch(e => {
        console.warn(e);
      });

    // #endif
  },
  onShow: function() {
    // 连接socket
    this.globalData.socket.initSocket();
  },
  onError(e) {
    console.warn("-----APP---", e);
  },
  methods: {
    initBar() {
      uni.getSystemInfo({
        success: function(e) {
          // #ifndef MP
          Vue.prototype.StatusBar = e.statusBarHeight;
          if (e.platform === "android") {
            Vue.prototype.CustomBar = e.statusBarHeight + 50;
          } else {
            Vue.prototype.CustomBar = e.statusBarHeight + 45;
          }
          // #endif

          // #ifdef MP-WEIXIN
          Vue.prototype.StatusBar = e.statusBarHeight;
          let custom = wx.getMenuButtonBoundingClientRect();
          Vue.prototype.Custom = custom;
          Vue.prototype.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
          // #endif

          // #ifdef MP-ALIPAY
          Vue.prototype.StatusBar = e.statusBarHeight;
          Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight;
          // #endif
        }
      });
    },
    // 初始化websocket
    initWebSocket() {
      let socket = new websocket({
        url: config.BASE_SOCKET_WSS,
        timeout: 2 * 60 * 1000,
        isSendHeart: true,
        params: {
          token: this.$store.state.user.TOKEN
        },
        debug: true,
        onSocketOpen: e => {
          // 发送token身份认证
          if (this.$store.state.user.TOKEN) {
            e.sendSocketMessage({
              c: "user",
              a: "register",
              token: this.$store.state.user.TOKEN
            });
          }
        },
        onSocketMessage: data => {
          if (typeof data !== "object") return;
          if (data.error === 1002) {
            this.$store.commit("user/LOGOUT");
            uni.showModal({
              title: "提示",
              content: "您尚未登录或登录已过期",
              confirmText: "去登录",
              cancelText: "返回",
              success: res => {
                if (res.confirm) {
                  uni.navigateTo({
                    url: "/pages/common/login"
                  });
                }
              }
            });
          } else if (data.error === 1003) {
            // 账户冻结
            uni.showModal({
              title: "提示",
              content: data.msg,
              confirmText: "确定",
              showCancel: false,
              success: res => {}
            });
          } else {
            if (data.cb) {
                  this.$store.commit(`${data.cb}`, data.error === 0 ? data.data : data.error);
              }
            }
        }
      });
      this.globalData.socket = socket;
      console.warn(socket);
    },
    // 初始化推送事件
    initPush() {
      // #ifdef APP-PLUS

      plus.push.setAutoNotification(true);
      //监听系统通知栏消息点击事件
      plus.push.addEventListener(
        "click",
        function(data) {
          //处理点击消息的业务逻辑代码
          console.warn("监听系统通知栏消息点击事件", data, data.payload);
          let payload = data.payload;
          if (uni.getSystemInfoSync().platform == "ios") {
            payload = JSON.parse(payload);
          }
          const openType = payload.openType || 0;
          // 如果0内页 1 tab页面
          if (openType === 0) {
            uni.navigateTo({ url: payload.url });
          } else {
            uni.switchTab({ url: payload.url });
          }
        },
        false
      );
      //监听接收透传消息事件iOS
      // TODO IOS无限推送bug
      if (uni.getSystemInfoSync().platform == "ios") {
        plus.push.addEventListener(
          "receive",
          function(data) {
            //处理透传消息的业务逻辑代码
            console.warn("处理透传消息的业务逻辑代码", data, data.payload);
            plus.push.createMessage(data.content, JSON.stringify(data.payload), {
              cover: false,
              title: data.title
            });
          },
          false
        );
      }
      // #endif
    }
  },
  globalData: {
    socket: null
  }
};
</script>

<style lang="scss">
@import "./static/styles.scss";








// 样式重置
.navigator-hover {
  background-color: inherit;
  opacity: 1;
}
/*每个页面公共css */
@import "./vender/colorui/main.css";
@import "./vender/colorui/animation.css";
@font-face {
  font-family: "ID";
  src: url("~@/static/fonts/Industry-DemiBold.ttf");
}
/* #ifndef APP-PLUS-NVUE */
page {
  height: 100%;
  width: 100%;
  position: relative;
  font-family: "ID";
}
/* #endif */

/* begin 重定义colorUI */
.bg-blue {
  background-color: #0f86c3;
}
.text-blue {
  color: #0f86c3;
}
.cu-form-group {
  position: relative;
  picker {
    padding-right: 60upx;
  }
  &.arrow {
    padding-right: 45px;
    &:before {
      position: absolute;
      top: 0;
      right: 15px;
      bottom: 0;
      display: block;
      margin: auto;
      width: 15px;
      height: 15px;
      color: #8799a3;
      content: "\E701";
      text-align: center;
      font-size: 17px;
      font-family: cuIcon;
      line-height: 15px;
    }
  }
}

/* end */
</style>
